package bintree.leetcode;

import java.util.Arrays;

public class Num462最少移动次数使元素相等 {
    public int minMoves2(int[] nums) {
        Arrays.sort(nums);
        int x = nums.length/2;
        int ret=0;
        for (int i = 0; i < nums.length; i++) {
            ret+=Math.abs(nums[i]-nums[x]);
            int z= 0; int y =0;
            if (nums[x]>=nums[i]) {
                z=nums[x]-nums[i];
            }else {
                y=nums[i]- nums[x];
            }
            ret+=(z+y);
        }
        return ret;
    }
}
